package test;



import java.time.Duration;

public class test4 {
    public void test(String str) {
        int[] nums = new int[4];
        if (str != null && str.length() != 0) {
            for (int i = 0; i < str.length(); i++) {
                if (isNum(str.charAt(i))) {
                    nums[0]++;
                } else if (isAlphabet(str.charAt(i))) {
                    nums[1]++;
                } else if (isBlank(str.charAt(i))) {
                    nums[2]++;
                } else {
                    nums[3]++;
                }
            }
        }

        System.out.println("数字： " + nums[0] + "个");
        System.out.println("字母： " + nums[1] + "个");
        System.out.println("空格： " + nums[2] + "个");
        System.out.println("其他： " + nums[3] + "个");
    }

    private boolean isNum(char c) {
        return c >= '0' && c <= '9';
    }

    private boolean isAlphabet(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }

    private boolean isBlank(char c) {
        return c == ' ';
    }
}
